/**
 * 作者:hanbo
 * 日期:2018/11/209:16
 * 描述:矢量图形绘制
 */
import {BaseBrush} from "./BaseBrush";

/**
 * 线类型片元着色器
 * @type {string}
 */
let FSHADER_SOURCE =
    `#ifdef GL_ES
     #ifdef GL_FRAGMENT_PRECISION_HIGH
     precision highp float;
     #else
     precision mediump float;
     #endif
     #endif
     varying vec4 v_Color;
     void main() {
         gl_FragColor = v_Color;
     }`;
/**
 * 线类型顶点着色器
 * @type {string}
 */
let VSHADER_SOURCE =
    `attribute vec4 a_Position;
     attribute vec4 a_Color;
     varying vec4 v_Color;
     uniform mat4 u_Matrix;
     void main() {
       gl_Position = u_Matrix * a_Position;
       v_Color = a_Color;
     }`;

export class VectorBrush extends BaseBrush{
    constructor(webGL,source_vs=VSHADER_SOURCE, source_fs=FSHADER_SOURCE) {
        super(webGL, source_vs, source_fs)
    }

    /**
     * 生成缓冲区，设置缓冲区读取方式
     * @param data {Float32Array}
     */
    flushData(data){
        this.createBuffer(data);
        const FSIZE = Float32Array.BYTES_PER_ELEMENT;
        let a_Position = this._webGL.getAttribLocation(this._program, 'a_Position');
        if (a_Position < 0) {
            console.error('获取a_Position存储位置失败');
            return -1;
        }
        let a_Color = this._webGL.getAttribLocation(this._program, 'a_Color');
        if (a_Color < 0) {
            console.error('获取a_Color存储失败');
            return -1;
        }
        this._webGL.vertexAttribPointer(a_Position, 3, this._webGL.FLOAT, false, FSIZE * 7, 0);
        this._webGL.enableVertexAttribArray(a_Position);
        this._webGL.vertexAttribPointer(a_Color, 4, this._webGL.FLOAT, false, FSIZE * 7, FSIZE * 3);
        this._webGL.enableVertexAttribArray(a_Color);
    }
}